import { NavigationActions, NavigationParams } from 'react-navigation';

let _navigator;

const setTopLevelNavigator = (navigatorRef) => {
  _navigator = navigatorRef;
};

const navigate = (routeName: string, params?: NavigationParams) => {
  if (_navigator) {
    _navigator.dispatch(
      NavigationActions.navigate({
        routeName,
        params,
      }),
    );
  }
};

const back = () => {
  if (_navigator) {
    _navigator.dispatch(NavigationActions.back());
  }
};

// add other navigation functions that you need and export them

export default {
  navigate,
  back,
  setTopLevelNavigator,
};
